import {getUserInfoByUserDDId} from '@/api/login'
import {selectMyPmById} from '@/api/myPerformance'
import { ref } from 'vue';

/**
 * 查询绩效所属者
 * @returns
 */
function useOwner() {
  let owner = ref<User.Info | undefined>(undefined)

  /**根据ownerDDId查询当前绩效所属用户信息 */
  function udpateOwner(data: {ownerDDId: string; success?: (data: User.Info) => void}) {
    getUserInfoByUserDDId({userDDId: data.ownerDDId})
      .then(res => {
        setOwner(res.data)
        data.success && data.success(res.data)
      })
      .catch(err => {})
  }

  /**根据pmId查询绩效所属人员 */
  function udpateOwnerByPmId(data: {pmId: number; success?: (data: MyPm.Pm) => void}) {
    selectMyPmById({pmId: data.pmId})
      .then(res => {
        udpateOwner({
          ownerDDId: res.data.pmUserDDId
        })
        data.success && data.success(res.data)
      })
      .catch(err => {})
  }

  function setOwner(data: User.Info) {
    owner.value = data
  }

  return {
    owner,
    udpateOwner,
    udpateOwnerByPmId,
    setOwner
  }
}

export default useOwner
